package users

import (
	"github.com/gin-gonic/gin"
	"go_auth/ext/app"
	"go_auth/ext/e"
	"go_auth/utils/commons"
	"net/http"

	"go_auth/ext/settings"
	"go_auth/servers/users"
)

func GetAllPer(c *gin.Context) {
	size := c.Query("size")
	pageNum := 0
	toInt, b := commons.StrToInt(size)
	if b {
		pageNum = toInt
	}

	user := users.Per{}
	per := user.GetAllPer(pageNum)
	var appG = app.Gin{C: c}
	code := e.Success

	if per == nil {
		code = e.EmptyError
	}
	appG.Response(http.StatusOK, code, per)
}

func Per(c *gin.Context) {
	id := c.PostForm("id")
	url := c.PostForm("url")
	desc := c.PostForm("desc")
	opt := c.PostForm("type")
	method := c.PostForm("method")

	var appG = app.Gin{C: c}
	code := e.Success

	per := users.Per{}
	per.Url = url
	per.Desc = desc
	per.Method = method

	if opt == settings.Add {
		if per.AddPer() == settings.Zero {
			code = e.AddError
		}
	} else if opt == settings.Delete {
		i, f := commons.StrToInt(id)
		if !f {
			appG.Response(http.StatusOK, e.AtoB, id)
			return
		}
		per.PerId = i
		if per.DeletePer() == settings.Zero {
			code = e.DeleteError
		}
	} else if opt == settings.Update {
		if per.UpdatePer() == settings.Zero {
			code = e.UpdateError
		}
	} else {
		code = e.ParamsError
	}

	appG.Response(http.StatusOK, code, id)
}
